/**
 * @class   wosh::Fact
 * @brief   Facts are the way any Event is broadcasted within WOSH system.
 *          They can be raised from Devices or Services, hardware or software.
 *          Usually it's Automation(manager) listening for Fact and generating
 *          effects, but also some GUI applications may listen them as notifiers.
 *          
 *          Some examples of facts:
 *           - Device turned on/off
 *
 *
 ****************************************************************************
 * @version 0.8.384 $Id: Fact.h 2833 2010-08-02 23:12:57Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_Fact_H__
 #define __WOSH_Core_Fact_H__

 #include <core/MessageContentGeneric.h>
   #include <core/List.h>

 
namespace wosh {


class Fact : public MessageContentGeneric
 {
	WOSH_SERIALIZABLE(wosh::Fact)
	WOSH_COMPARE_RTTI(wosh::Fact)


	public:
		/**
		 * @brief  Empty constructor.
		 */
		Fact();
		/**
		 * @brief  Constructor with initializer(s), provided for convenience.
		 * @param [in] eventname the event string to be assigned
		 */
		explicit Fact( const std::string& eventname );
		/**
		 * @brief  Constructor with initializer(s), provided for convenience.
		 * @param [in] eventname the event string to be assigned
		 * @param [in] data the data object (ISerializable abstraction) associated with the event. May be NULL.
		 */
		explicit Fact( const std::string& eventname, ISerializable* data );
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		Fact( const Fact& m );
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~Fact() { }


/** @name Getters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Get the event (string) associated with the event
		 * @return constant STL string reference (to internal field)
		 */
		inline const std::string& getEvent() const						{ return this->contentContext; }

		/**
		 * @brief  Get MessageContent::TYPE of this implementation: a FACT
		 * @return MessageContent::FACT
		 */
		virtual MessageContent::TYPE getType() const					{ return MessageContent::FACT; }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Set the event (string) associated with the event
		 * @param [in] value the event string to be assigned
		 * @see    setContentContext()
		 */
		inline void setEvent( const std::string& value ) 				{ this->contentContext = value; }

//@}
/** @name Helpers
 * @{
 ******************************************************************************/
	public:
		template <class OBJ_TYPE>
		const OBJ_TYPE* getDataItem( unsigned int index ) const {
			if ( !hasData() ) return NULL;
			if ( index == 0 && getData()->isKindOf<OBJ_TYPE>() )
				return getData()->as<OBJ_TYPE>();
			if ( getData()->isKindOf<List>() ) {
				const List* list = getData()->as<List>();
				if ( list == NULL ) return NULL;
				if ( (unsigned int)list->size() <= index ) return NULL;
				if ( list->itemAt(index) == NULL ) return NULL;
				return list->itemAt(index)->as<OBJ_TYPE>();
			 }
			return NULL;
		 }
//@}
/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:
		virtual Fact& operator=(const Fact& m);
		virtual bool operator==(const Fact &other) const;
		virtual bool operator!=(const Fact &other) const			{ return !(*this == other);  }
//@}

}; // class def


 #define _EVENT_ListChanged		"ListChanged"

 class ISerializable;

/**
 * @class   wosh::Fact_ListChanged
 * @brief   Raised when 
 *
 * Internally, data is stored as wosh::Variant (STRING).
 *
 * Fields are:
 *  - \c 
 *

 * \code
 * \endcode
 *
 * @see     wosh::Fact
 * @see     wosh::WoshHost
 ****************************************************************************
 * @version 0.8.399 $Id: Fact.h 2833 2010-08-02 23:12:57Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
class Fact_ListChanged : public Fact
 {
	WOSH_SERIALIZABLE(wosh::Fact_ListChanged)

	public:
		enum FACT_LIST_EVENT {
			EVENT_LISTITEM_UNKNOWN		= 0x00,
			EVENT_LISTITEM_ADDED		= 0x01,
			EVENT_LISTITEM_UPDATED		= 0x02,
			EVENT_LISTITEM_REMOVED		= 0x04,
		 };

	public:
		/**
		 * @brief  Empty constructor.
		 */
		Fact_ListChanged( const char* method_name = _EVENT_ListChanged )
			: Fact( method_name ) { }
		/**
		 * @brief  Constructor with initializers.
		 * @param woshname [in] the subject (host) of the event
		 */
		Fact_ListChanged( ISerializable* item, FACT_LIST_EVENT list_event, const char* method_name = _EVENT_ListChanged );

		Fact_ListChanged( const Variant& item_key, FACT_LIST_EVENT list_event, const char* method_name = _EVENT_ListChanged );

		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		Fact_ListChanged( const Fact_ListChanged& m )
			: Fact(m) { }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~Fact_ListChanged() { }

	public:
		const ISerializable* getItem() const;
		const Variant* getItemKey() const;

		int getListAction() const;

		inline bool isItemAdded() const				{ return ( getListAction() == (int)EVENT_LISTITEM_ADDED ); }
		inline bool isItemUpdated() const			{ return ( getListAction() == (int)EVENT_LISTITEM_UPDATED ); }
		inline bool isItemremoved() const			{ return ( getListAction() == (int)EVENT_LISTITEM_REMOVED ); }

	public:
		virtual std::string toString() const;

	public:
		static const char* getListEventAsString( FACT_LIST_EVENT list_event );

}; // class def


}; // namespace wosh

#endif //__WOSH_Core_Fact_H__
